package com.snow.controller;


import com.github.pagehelper.PageInfo;
import com.snow.constant.Types;
import com.snow.constant.Webconst;
import com.snow.pojo.dto.ArchiveDto;
import com.snow.pojo.dto.cond.ContentCond;
import com.snow.pojo.model.CommentDomain;
import com.snow.pojo.model.ContentDomain;
import com.snow.service.comment.CommentService;
import com.snow.service.content.ContentService;
import com.snow.service.site.SiteService;
import com.snow.utils.Commons;
import com.snow.utils.DateKit;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-09
 **/
@Controller
public class HomeController extends BaseController {
    @Autowired
    private ContentService contentService;
    @Autowired
    private Commons commons;
    @Autowired
    private CommentService commentService;
    @Autowired
    private SiteService siteService;

    @GetMapping(value={"/blog","/blog/index"})
    public String blogindex(
            HttpServletRequest request,
            @RequestParam(name="limit",required = false,defaultValue = "4")
            Integer limit
    ){
        return this.blogindex(request,1,limit);
    }
    @ApiOperation("博客首页-分页")
    @GetMapping("/blog/page/{p}")
    public String blogindex(
        HttpServletRequest request,
        @PathVariable("p") int p,
        @RequestParam(name="limit",required = false,defaultValue = "4")
        int limit
    ){
        p = p < 1 || p > Webconst.MAX_PAGE ? 1 : p;
        ContentCond contentCond = new ContentCond();
        contentCond.setType(Types.ARTICLE.getType());
        PageInfo<ContentDomain> articles = contentService.getArticlesByCond(contentCond, p, limit);
        request.setAttribute("active","blog");
        request.setAttribute("articles",articles);
        request.setAttribute("types","article");
        request.setAttribute("commons",commons);
        return "site/blog";
    }

    @ApiOperation("作品主页")
    @GetMapping(value={"","/index"})
    public String index(
            HttpServletRequest request,
            @RequestParam(name="limit",required = false,defaultValue = "11")
            Integer limit
    ){
        return this.indexpage(request,limit,1);
    }
    @ApiOperation("作品主页-分页")
    @GetMapping("/photo/page/{p}")
    public String indexpage(
            HttpServletRequest request,
            @RequestParam(name="limit",required = false,defaultValue = "50")
            Integer limit,
            @PathVariable("p")
            Integer p
    ){
        p = p < 0 || p > Webconst.MAX_PAGE ? 1 : p;
        ContentCond contentCond = new ContentCond();
        contentCond.setType(Types.PHOTO.getType());

        PageInfo<ContentDomain> articles = contentService.getArticlesByCond(contentCond, p, limit);
        request.setAttribute("archives",articles);
        request.setAttribute("commons",commons);
        request.setAttribute("active","work");
        return "site/index";

    }
    @ApiOperation("文章内容页")
    @GetMapping("/blog/article/{cid}")
    public String article(
            Model model,
            @PathVariable("cid")
            Integer cid
    ){
        ContentDomain article = contentService.getArticleById(cid);
       //更新文章的点击数据\
        this.updateArticleHit(cid,article.getHits());
        List<CommentDomain> comments = commentService.getCommentsByCid(cid);
        model.addAttribute("article",article);
        model.addAttribute("comments",comments);
        model.addAttribute("commons",commons);
        model.addAttribute("active","blog");
        return "site/blog-details";
    }

    private void updateArticleHit(Integer cid,Integer chits){
        Integer hits = cacheMap.getHashCache("article", "hits");
        if(chits == null){
            chits = 0;
        }
        hits = hits == null ? 1 : (hits + 1);
        if(hits > Webconst.HITS_CACHE){
            ContentDomain content = new ContentDomain();
            content.setHits(chits + hits);
            contentService.updateArticleHits(content);
            cacheMap.setHashCache("article","hits",1);
        }
        else{
            cacheMap.setHashCache("article","hits",hits);
        }
    }


    @ApiOperation("归档页-按日期")
    @GetMapping("/blog/archives/{date}")
    public String archivesDate(
            Model model,
            @PathVariable("date")
            String date
    ){
        Date sd = DateKit.dateFormat(date, "yyyy年MM月");
        int start = DateKit.getUnixTimeByDate(sd);
        int end = DateKit.getUnixTimeByDate(DateKit.dateAdd(DateKit.INTERVAL_MONTH,sd,1)) - 1;
        ContentCond contentCond = new ContentCond();
        contentCond.setStartTime(start);
        contentCond.setEndTime(end);
        contentCond.setType(Types.ARTICLE.getType());
        List<ArchiveDto> archive = siteService.getArchive(contentCond);
        model.addAttribute("archives_list",archive);
        model.addAttribute("commons",commons);
        return "blog/archives";
    }
    @ApiOperation("分档页-按年份")
    @GetMapping("/blog/archives/{year}")
    public String archivesYear(
            Model model,
            @PathVariable("year")
            String year
    )
    {
        ContentCond contentCond = new ContentCond();
        int start = DateKit.getUnixTimeByDate(DateKit.getYearStartDay(year,"yyyy"));
        int end = DateKit.getUnixTimeByDate(DateKit.getYearEndDay(year,"yyyy"));
        contentCond.setStartTime(start);
        contentCond.setEndTime(end);
        contentCond.setType(Types.ARTICLE.getType());
        List<ArchiveDto> archive = siteService.getArchive(contentCond);
        model.addAttribute("archives_list",archive);
        model.addAttribute("commons",commons);
        return "blog/archives";
    }
    @ApiOperation("归档页")
    @GetMapping("/blog/archives")
    public String archives(Model model){
        ContentCond contentCond = new ContentCond();
        contentCond.setType(Types.ARTICLE.getType());
        List<ArchiveDto> archive = siteService.getArchive(contentCond);
        model.addAttribute("commons",commons);
        model.addAttribute("archives_list",archive);
        return "site/archives";
    }
    @ApiOperation("分类页-分页")
    @GetMapping("/blog/categories/{category}/page/{page}")
    public String categories(
            Model model,
            @PathVariable("category")
            String category,
            @RequestParam(name="page",required = false,defaultValue = "1")
            Integer page,
            @RequestParam(name="limit",required = false,defaultValue = "12")
            Integer limit
    ){
        ContentCond contentCond = new ContentCond();
        contentCond.setType(Types.ARTICLE.getType());
        contentCond.setCategory(category);
        PageInfo<ContentDomain> articles = contentService.getArticlesByCond(contentCond, page, limit);
        model.addAttribute("commons",commons);
        model.addAttribute("articles_list",articles);
        model.addAttribute("type","categories");
        model.addAttribute("param_name",category);
        return "blog/category";
    }
    @ApiOperation("作品内容")
    @GetMapping(value = "/photo/article/{cid}")
    public String article(
            @PathVariable("cid")
                    Integer cid,
            HttpServletRequest request
    ){
        ContentDomain article = contentService.getArticleById(cid);
        request.setAttribute("archive", article);
        request.setAttribute("active","work");
        request.setAttribute("commons",commons);
        return "site/works-details";
    }
    @GetMapping("/about")
    public String about(Model model){
        this.blogPageBaseData(model,null);
        model.addAttribute("commons",commons);
        model.addAttribute("actived","about");
        return "site/about";
    }




}
